﻿using Core.Log;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Language
{
    /// <summary>
    /// 无资源缓存
    /// </summary>
    public static class NoResourceCache
    {
        private static object _lock = new object();

        public static List<NoResourceCacheData> Data
        {
            get;
            private set;
        } = new List<NoResourceCacheData>();

        /// <summary>
        /// 添加数据项
        /// </summary>
        /// <param name="key"></param>
        /// <param name="cultureInfo"></param>
        /// <param name="dateTime"></param>
        public static void AddDataItem(string key, CultureInfo? cultureInfo,DateTime? dateTime)
        {
            if(!Data.Any(p=>p.Key == key && p?.CultureInfo?.Name == cultureInfo?.Name))
            {
                lock (_lock)
                {
                    if (!Data.Any(p => p.Key == key && p?.CultureInfo?.Name == cultureInfo?.Name))
                    {
                        if(key.Equals("{DependencyProperty.UnsetValue}"))
                        {
                            return;
                        }
                        Data.Add(new NoResourceCacheData(key, cultureInfo,dateTime));
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine($"语言资源中缺少本地化语言翻译：");
                        sb.AppendLine($"Key => {key}");
                        sb.AppendLine($"CultureInfo => {cultureInfo?.Name}");
                        //sb.AppendLine($"TryParents => {tryParents}");
                        AppTrace.Logger.LogInformation(sb.ToString());
                    }
                }
            }
        }


        /// <summary>
        /// 移除数据项
        /// </summary>
        /// <param name="key"></param>
        /// <param name="cultureInfo"></param>
        /// <param name="dateTime"></param>
        public static void RemoveDataItem(string key, CultureInfo? cultureInfo)
        {
            if (Data.Any(p => p.Key == key && p?.CultureInfo?.Name == cultureInfo?.Name))
            {
                lock (_lock)
                {
                    if (!Data.Any(p => p.Key == key && p?.CultureInfo?.Name == cultureInfo?.Name))
                    {
                        Data.RemoveAll(p => p.Key == key && p?.CultureInfo?.Name == cultureInfo?.Name);
                    }
                }
            }
        }

        /// <summary>
        /// 清空数据
        /// </summary>
        public static void ClearData()
        {
            if(Data?.Count != 0)
            {
                lock (_lock)
                {
                    if(Data?.Count != 0)
                    {
                        Data?.Clear();
                    }
                }
            }
        }
    }

    public class NoResourceCacheData
    {
        public NoResourceCacheData(string? key, CultureInfo? cultureInfo, DateTime? recordTime)
        {
            Key = key;
            CultureInfo = cultureInfo;
            if(recordTime == null)
            {
                recordTime = DateTime.Now;
            }
            RecordTime = recordTime;
        }


        /// <summary>
        /// 资源键
        /// </summary>
        public string? Key { get; set; }

        /// <summary>
        /// 资源键
        /// </summary>
        public CultureInfo? CultureInfo { get; set; }

        /// <summary>
        /// 记录时间
        /// </summary>
        public DateTime? RecordTime { get; set; } = DateTime.Now;
    }
}
